package cn.itcast.bos.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;


import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.AreaService;
import cn.itcast.bos.utils.PinYin4jUtils;
import cn.itcast.bos.web.action.common.BaseAction;
@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class AreaAction extends BaseAction<Area> {

	private static final long serialVersionUID = 1L;
	@Autowired
	private AreaService areaService;
	/**
	 * 添加
	 * 
	 */
	//接受上传文件
	private File file;
	private String  fileFileName;
	private String fileContentType;
	
	
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public void setFile(File file) {
		this.file = file;
	}

	@Action(value="area_import",results= {@Result(name="success",type="json")})
	public String importArea() throws Exception {
		System.out.println("file + "+ file);
		//基于.xls格式解析HSSf 
		//1 加载Excel表格
		HSSFWorkbook hssfWorkbook =new HSSFWorkbook(new FileInputStream(file));
		//2 读取一个sheet
		HSSFSheet hssfSheet=hssfWorkbook.getSheetAt(0);
		//3  读取sheet中每一行, 
		for (Row row : hssfSheet) {
			//第一行表头跳过
			if (row.getRowNum()==0) {
				continue;
			}
			//跳过空值
			if (row.getCell(0)==null||StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			List<Area> areas=new ArrayList<Area>();
			/**基于pinyin4f 生成城市编码和简码*/
			String province = area.getProvince();
			String city = area.getCity();
			String district = area.getDistrict();
			province=province.substring(0, province.length()-1);//去掉省
			city=city.substring(0, city.length()-1);        //去掉市
			district=district.substring(0, district.length()-1);//去掉区/县
			//生成简码
			String[] headArray = PinYin4jUtils.getHeadByString(province+city+district);
			StringBuffer buffer = new StringBuffer();
			for (String headStr : headArray) {
				buffer.append(headStr);
				
			}
			String shortcode = buffer.toString();
			area.setShortcode(shortcode);
			
			//生成城市编码
			String citycode = PinYin4jUtils.hanziToPinyin(city, "");
			area.setCitycode(citycode);
			
			
			areas.add(area);
			//调用也去层
			areaService.saveArea(areas);
			
		}
		return SUCCESS;
	}
	
	/**
	 * 没有条件的分页查询
	 */
	@Action(value="area_pageQuery",results= {@Result(name="success",type="json")})
	public String pageQuery() {
		/**
		 * 有条件查询
		 * 
		 */
		//调用service层,查询返回数据
		Pageable pageable=new PageRequest(page-1, rows);
		//组织查询条件
		Specification<Area> specification=new Specification<Area>() {

			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				 Predicate predicate =null;
				List<Predicate> list=new ArrayList<Predicate>();
				//省份
				if (StringUtils.isNotBlank(model.getProvince())) {
					Predicate p = cb.like(root.get("province").as(String.class), "%"+model.getProvince()+"%");
					list.add(p);
				}
				//市
				if (StringUtils.isNotBlank(model.getCity())) {
					Predicate p = cb.like(root.get("city").as(String.class), "%"+model.getCity()+"%");
					list.add(p);
				}
				//区
				if (StringUtils.isNotBlank(model.getDistrict())) {
					Predicate p = cb.like(root.get("district").as(String.class), "%"+model.getDistrict()+"%");
					list.add(p);
				}
				if (list!=null&&list.size()>0) {
					Predicate [] p=new Predicate[list.size()];
					predicate=cb.and(list.toArray(p));
				}
				return predicate;
			}
		};
		Page<Area> pageData= areaService.findPageDataArea(specification, pageable);                  
		//返回客户端数据
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}
	
	

}

















